/*
采用 getaddrinfo 传入主机名和服务名实现的 UDP 时间获取客户端。
**/

#include <define.h>
#include <error.h>
#include <wrapfunc.h>
#include <sockfunc.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stddef.h>
#include <stdlib.h>


int
main(int argc, char **argv) {
    if (argc != 3)
        error_quit("usage: %s host service", argv[0]);
    const char *host = argv[1];
    const char *service = argv[2];

    struct sockaddr *saptr;
    socklen_t len;
    int sockfd = Udp_client(host, service, &saptr, &len);
    Sendto(sockfd, "", 1, 0, saptr, len);
    free(saptr);

    char buf[MAX_LINE];
    struct sockaddr_storage ss;
    len = sizeof(ss);
    ssize_t n = Recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&ss, &len);
    buf[n] = 0;
    error_msg("recv datagram from:%s server time:%s", sock_ntop((struct sockaddr*)&ss, len), buf);
    Close(sockfd);
    
    return 0;
}
